<?php

namespace App\Admin\Controllers;

use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Admin;
use App\Admin\Extensions\ShowArtwork;

class UsersController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);
        $grid->column('id', __('Id'));
        $grid->column('created_at', __('创建时间'));
//        $grid->column('updated_at', __('修改时间'));
//        $grid->column('username', __('用户名'))->display(function($username){
//            $user = User::where('username',$username)->first('pid');
//            if($user->pid != 0){
//                $first = User::where('id',$user->pid)->first();
//                $content = '| 一：'.$first->username ;
//                if($first->pid != 0){
//                    $second = User::where('id',$first->pid)->first();
//                    $content.= ' 二：'.$second->username;
//                }
//            }else{
//                $content = '';
//            }
//            return $username.$content;
//        });
        $grid->column('username', __('用户名'));//->popover('right');
        $grid->column('mobile', __('手机号'));
        $grid->column('balance', __('余额'))->sortable();
       // $grid->column('btc', __('比特币'));
        $grid->column('recommend', __('推荐收益'))->sortable();
        $grid->column('push_num', __('直推数量'));
        $grid->column('team_num', __('团队数量'));
        $grid->column('is_real', __('是否实名'));
        $grid->column('real_name', __('真实姓名'));
        $grid->column('id_number', __('身份证号'));
        $grid->column('bank_card', __('银行卡'));
     //   $grid->column('btc_address', __('比特币地址'));
        $grid->column('status', __('状态'))->display(function ($status) {
            return config('conf.status')[$status];
        });
//        $grid->column('order', __('算力'))->display(function ($order) {
//            $a = 0;
//            foreach($order as $v){
//                 $a +=$v['computing'];
//            }
//            return $a;
//        })->sortable();

//        $grid->tools(function ($tools) {
//            $url = "/admin/users";
//            $icon = "fa-eye";
//            $text = "发放推荐收益";
//            $tools->append(new ShowArtwork($url,$icon,$text));
//        });


//        $grid->filter(function($filter){
//            // 去掉默认的id过滤器
//            $filter->disableIdFilter();
//            $users = User::all()->toArray();
//            $users = array_column($users,'username','id');
//            $filter->equal('id','用户名')->select($users);
//        });


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('password', __('Password'));
        $show->field('remember_token', __('Remember token'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('username', __('Username'));
        $show->field('mobile', __('Mobile'));
        $show->field('pid', __('Pid'));
        $show->field('balance', __('Balance'));
       // $show->field('btc', __('Btc'));
       // $show->field('electricity_fee', __('Electricity fee'));
        $show->field('push_num', __('Push num'));
        $show->field('team_num', __('Team num'));
        $show->field('is_real', __('Is real'));
        $show->field('real_name', __('Real name'));
        $show->field('id_number', __('Id number'));
        $show->field('bank_card', __('Bank card'));
      //  $show->field('btc_address', __('Btc address'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);

        //$form->password('password', __('密码'))->style('display:none')->default('123');
        $form->text('username', __('用户名'));
        $form->mobile('mobile', __('手机号'));
        $form->number('pid', __('上级用户'));
        $form->decimal('balance', __('余额'))->default(0.00000);
    //    $form->decimal('btc', __('比特币'))->default(0.0000000000);
      //  $form->decimal('electricity_fee', __('电费'))->default(0.00000);
        $form->number('push_num', __('直推数量'));
        $form->number('team_num', __('团队数量'));
        $form->number('is_real', __('是否直推'));
        $form->text('real_name', __('真实姓名'));
        $form->text('id_number', __('身份证号'));
        $form->text('bank_card', __('银行卡'));
     //   $form->text('btc_address', __('比特币地址'));
        $form->select('status', __('用户状态'))->options(config('conf.status'));;

        return $form;
    }
}
